﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BrightIdeasSoftware;
using MoneyWentWhere.DataModel;
using System.Windows.Forms;
using System.Collections;
using System.Drawing;

namespace MoneyWentWhere.WinFormsUI.Reports
{
    public abstract class ReportType
    {
        TreeListViewX _view;
        MoneyDataSet _data;

        protected abstract Type RowType { get; }

        /// <summary>
        /// Create the view control, set up properties and events
        /// </summary>
        /// <returns></returns>
        public virtual ObjectListView CreateView()
        {
            _view = new TreeListViewX();
            _view.CanExpandGetter = CanExpand;
            _view.ChildrenGetter = GetChildren;
            _view.RowFormatter = FormatRow;

            Generator.GenerateColumns(_view, RowType);
            return _view;
        }

        public ObjectListView View { get { return _view; } }

        /// <summary>
        /// Fill the view with data (or refresh)
        /// </summary>
        /// <param name="data"></param>
        public virtual void FillView(MoneyDataSet data)
        {
            _data = data;
            if (_view == null) { throw new InvalidOperationException("View is null."); }

            _view.ClearObjects();
            _view.AddObjects(GetViewObjects(_data));
        }

        protected abstract ICollection GetViewObjects(MoneyDataSet dataSet);

        protected virtual bool CanExpand(object model)
        {
            IReportRow row = model as IReportRow;
            if (row == null) { return false; }
            return row.NodeCount > 0;
        }

        protected virtual IEnumerable GetChildren(object model)
        {
            IReportRow row = model as IReportRow;
            if (row == null) { return null; }
            return row.NodeObjects;
        }

        protected virtual void FormatRow(OLVListItem olvItem)
        {
            if (CanExpand(olvItem.RowObject)) { olvItem.BackColor = Color.LightBlue; }
        } 

    }


}
